package model;

import android.widget.ImageView;

public class GameField {

	/**
	 * createField method is broken. It's from the console version
	 */

	// /**
	// * The next method createField returns array with all the Lines, Dots and
	// * Squares. Sets Lines and Squares references to surrounding squares and
	// * lines.
	// */
	//
	// public static ImageView[][] createField(int inputRows, int inputCols) {
	//
	// ImageView[][] field = new ImageView[inputRows * 2 - 1][inputCols * 2 -
	// 1];
	//
	// for (int row = 0; row < field.length; row++) {
	// for (int col = 0; col < field[row].length; col++) {
	//
	// if (row % 2 == 0) {
	// if (col % 2 == 0) {
	// field[row][col] = new Dot();
	// } else {
	// field[row][col] = new LineHorizontal();
	// }
	// } else {
	// if (col % 2 == 0) {
	// field[row][col] = new LineVertical();
	// } else {
	// field[row][col] = new Square();
	// }
	// }
	// }
	// }
	//
	// setReferences(field);
	//
	// return field;
	//
	// }

	/**
	 * The next code is for setting the references between squares and lines in
	 * an array of ImageView objects. catches are empty but in this case it's OK
	 */

	public static void setReferences(ImageView[][] field) {

		for (int row = 0; row < field.length; row++) {
			for (int col = 0; col < field[row].length; col++) {

				if (row % 2 == 0) {
					if (col % 2 != 0) {

						try {
							((LineHorizontal) field[row][col])
									.setUpperSquare((Square) field[row - 1][col]);
						} catch (ArrayIndexOutOfBoundsException e) {
							// e.printStackTrace();
						}

						try {
							((LineHorizontal) field[row][col])
									.setDownSquare((Square) field[row + 1][col]);
						} catch (ArrayIndexOutOfBoundsException e) {
							// e.printStackTrace();
						}
					}
				} else {
					if (col % 2 == 0) {

						try {
							((LineVertical) field[row][col])
									.setRightSquare((Square) field[row][col + 1]);
						} catch (ArrayIndexOutOfBoundsException e) {
							// e.printStackTrace();
						}

						try {
							((LineVertical) field[row][col])
									.setLeftSquare((Square) field[row][col - 1]);
						} catch (ArrayIndexOutOfBoundsException e) {
							// e.printStackTrace();
						}

					} else {

						// here no catch-block is needed because we are sure we
						// have square surrounding objects

						((Square) field[row][col])
								.setSurroundingLineReferences(
										(LineVertical) field[row][col - 1],
										(LineVertical) field[row][col + 1],
										(LineHorizontal) field[row - 1][col],
										(LineHorizontal) field[row + 1][col]);
					}
				}
			}
		}
	}
}
